package com.example.another.bottomnavigationdemo1;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.another.bottomnavigationdemo1.fragment.FirstFragment;
import com.example.another.bottomnavigationdemo1.fragment.FiveFragment;
import com.example.another.bottomnavigationdemo1.fragment.FourFragment;
import com.example.another.bottomnavigationdemo1.fragment.SecondFragment;
import com.example.another.bottomnavigationdemo1.fragment.ThirdFragment;


/***
 * 参考博客：官方的博客
 * https://github.com/Ashok-Varma/BottomNavigation
 *
 * 别人的博客：
 * https://blog.csdn.net/u010046908/article/details/50962081
 */
public class ForthActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {
    private static final String TAG = "ForthActivity";
    private BottomNavigationBar mBottomNavigationBar;
    private Fragment mFragment1, mFragment2, mFragment3, mFragment4, mFragment5;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forth);
        initViews();
    }

    private void initViews() {


        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.data_analysis_selected, "menu1"))
                .addItem(new BottomNavigationItem(R.drawable.personal_information_selected, "menu2"))
                .addItem(new BottomNavigationItem(R.drawable.identify_selected, "menu3"))
                .addItem(new BottomNavigationItem(R.drawable.search_selected, "menu4"))
                .addItem(new BottomNavigationItem(R.drawable.history_selected, "menu5"))
                .initialise();
        mBottomNavigationBar.setTabSelectedListener(this);

        setDefaultFragment();
    }

    private void hiddFragments(FragmentTransaction fragmentTransaction) {

        if (mFragment1 != null) {
            fragmentTransaction.hide(mFragment1);
        }
        if (mFragment2 != null) {
            fragmentTransaction.hide(mFragment2);
        }

        if (mFragment3 != null) {
            fragmentTransaction.hide(mFragment3);
        }

        if (mFragment4 != null) {
            fragmentTransaction.hide(mFragment4);
        }

        if (mFragment5 != null) {
            fragmentTransaction.hide(mFragment5);
        }
    }

    private void setDefaultFragment() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        if (mFragment1 == null) {
            mFragment1 = new FirstFragment();
        }
        fragmentTransaction.add(R.id.fragment_container4, mFragment1);
        fragmentTransaction.commit();
    }


    @Override
    public void onTabSelected(int position) {
        Log.d(TAG, "onTabSelected: " + position);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hiddFragments(fragmentTransaction);
        switch (position) {
            case 0:
                if (mFragment1 == null) {
                    mFragment1 = new FirstFragment();
                    fragmentTransaction.add(R.id.fragment_container4, mFragment1);
                } else {
                    fragmentTransaction.show(mFragment1);
                }
                break;
            case 1:
                if (mFragment2 == null) {
                    mFragment2 = new SecondFragment();
                    fragmentTransaction.add(R.id.fragment_container4, mFragment2);
                } else {
                    fragmentTransaction.show(mFragment2);
                }
                break;
            case 2:
                if (mFragment3 == null) {
                    mFragment3 = new ThirdFragment();
                    fragmentTransaction.add(R.id.fragment_container4, mFragment3);
                } else {
                    fragmentTransaction.show(mFragment3);
                }
                break;
            case 3:
                if (mFragment4 == null) {
                    mFragment4 = new FourFragment();
                    fragmentTransaction.add(R.id.fragment_container4, mFragment4);
                } else {
                    fragmentTransaction.show(mFragment4);
                }
                break;
            case 4:
                if (mFragment5 == null) {
                    mFragment5 = new FiveFragment();
                    fragmentTransaction.add(R.id.fragment_container4, mFragment5);
                } else {
                    fragmentTransaction.show(mFragment5);
                }
                break;
            default:
                break;
        }
        fragmentTransaction.commit();

    }

    @Override
    public void onTabUnselected(int position) {
        Log.d(TAG, "onTabUnselected: " + position);
    }

    @Override
    public void onTabReselected(int position) {
        Log.d(TAG, "onTabReselected: " + position);
    }
}
